package com.stlm2.controller;

import com.stlm2.dems.service.InventoryLogService;
import com.stlm2.dems.service.InventoryService;
import com.stlm2.dems.service.ValueAddedServicesService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/10/18.
 */
@Controller
@RequestMapping("/inventoryC")
public class InventoryController {
    @Resource
    private InventoryService inventoryService;
    @Resource
    private InventoryLogService inventoryLogService;
    /*zlq
* crm查询库存商品列表页面
* */
    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/inventory/list");
        return modelAndView;
    }

    /*zlq
* crm商品详情页面
* */
    @RequestMapping(value="/detailsView",produces="application/json;charset=UTF-8")
    public ModelAndView detailsView(ModelAndView modelAndView,String id){
        modelAndView.setViewName("/inventory/details");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /*
   *CRM查询库存列表
   * zlq
   * token:
   * page:页码
   * shopId：店铺ID
   * code：商品编码
   * name：商品名称
   * abbreviation：商品简称
   * state：商品状态 true：有效 false：无效 空为全部
    */
    @ResponseBody
    @RequestMapping(value="/getList.json",produces="application/json;charset=UTF-8")
    public  String getList(ModelMap model, String page,String shopId,String token,String code ,String name,String abbreviation,Boolean state){
        String data=null;
        try {
            data=inventoryService.getList( model,page,shopId,token,code ,name,abbreviation,state);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }


    /*
    *商品入库
    * zlq
    * token：
    * id：库存记录ID
    * inventorySourceValue: 来源
    * changeNum:改变的值
    * remark:备注
     */
    @ResponseBody
    @RequestMapping(value="/goodsBePutInStorage.json",produces="application/json;charset=UTF-8")
    public  String goodsBePutInStorage(ModelMap model,String id,String token,String inventorySourceValue,String changeNum,String remark){
        String data=null;
        try {
            data=inventoryService.goodsBePutInStorage(model,id,token,inventorySourceValue,changeNum,remark);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"商品入库失败",data);
        }
        return  data;
    }


    /*
*商品出库
* zlq
* token：
* inventorySourceValue: 来源
* changeNum:改变的值
* id：库存ID
* remark:备注
 */
    @ResponseBody
    @RequestMapping(value="/goodsOutput.json",produces="application/json;charset=UTF-8")
    public  String goodsOutput(ModelMap model,String token,String inventorySourceValue,String changeNum,String id,String remark) {
        String data=null;
        try {
            data=inventoryService.goodsOutput(model,token,inventorySourceValue,changeNum,id,remark) ;
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"商品出库失败",data);
        }
        return  data;
    }

    /*
    *获取库存商品详情
    * zlq
    * token:
    *id:库存ID
     */
    @ResponseBody
    @RequestMapping(value="/getInventoryDetails.json",produces="application/json;charset=UTF-8")
    public  String getInventoryDetails(ModelMap model,String id,String token){
        String data=null;
        try {
            data=inventoryService.getInventoryDetails(model,id,token);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }

    /*
*获取库存日志列表
* zlq
* page:页码
* token：
* inventoryId：库存记录ID
* typeValue：类型 1：+ 2：-
* inventorySourceValue：来源 1：采购 2：线下核销 3：订单购买 4： 取消订单 5：驳回订单 6：自营调货 7：库存盘点
 */
    @ResponseBody
    @RequestMapping(value="/getLogList.json",produces="application/json;charset=UTF-8")
    public  String getLogList(ModelMap model, String page, String token, String inventoryId,String typeValue,String inventorySourceValue){
        String data=null;
        try {
            data=inventoryLogService.getLogList(model,page,token,inventoryId,typeValue,inventorySourceValue);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }
}
